﻿using FriendStar.Code;
using FriendStar.Domain.Entity.WareHouse;
using FriendStar.Domain.IRepository.WareHouse;
using FriendStar.Repository.WareHouse;
using System.Collections.Generic;
using System.Linq;
using SqlSugarRepository;
using FriendStar.Application.SystemManage;
using FriendStar.Data;
using System;
using FriendStar.Application.BaseData;
using FriendStar.Domain.Entity.BaseData;

namespace FriendStar.Application.WareHouse
{
    public class CPK_11_DJ_StoRegisterApp
    {
        private ICPK_11_DJ_StoRegisterRepository service = new CPK_11_DJ_StoRegisterRepository();
        private ModuleApp moduleApp = new ModuleApp();
        private ModuleButtonApp moduleButtonApp = new ModuleButtonApp();
        private CPK_51_JG_StoCPKPriceApp cpk_51_JG_StoCPKPriceApp = new CPK_51_JG_StoCPKPriceApp();
        private CWB_JG_21_StoSettlePriceApp cwb_JG_21_StoSettlePriceApp = new CWB_JG_21_StoSettlePriceApp();
        private readonly string useUnit= Configs.GetValue("UseUnit");
        private readonly string userName = OperatorProvider.Provider.GetUserName();

        public List<CPK_11_DJ_StoRegisterEntity> GetList(string keyword = "")
        {
            var expression = ExtLinq.True<CPK_11_DJ_StoRegisterEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.UseUnit.Contains(keyword));
                expression = expression.Or(t => t.FDouBarCode.Contains(keyword));
                expression = expression.Or(t => t.FDocuTypeNO.Contains(keyword));
            }
            //expression = expression.And(t => t.F_Category == 1); 
            return service.IQueryable(expression).OrderBy(t => t.UseUnit).ToList();
            //return service.IQueryable().OrderBy(t => t.FRegisterID).ToList();
        }
 
        public CPK_11_DJ_StoRegisterEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(p => p.FRegisterID == keyValue);
        }
        public void SubmitForm(CPK_11_DJ_StoRegisterEntity cpkEntity,string keyValue)
        {
            if (string.IsNullOrEmpty(cpkEntity.FRegisterID))
            {
                CPK_51_JG_StoCPKPriceEntity priceEntity = cpk_51_JG_StoCPKPriceApp.GetEntity(cpkEntity.FProductionNO, cpkEntity.FProCompanyNO, cpkEntity.FProductionTypeNO);
                CWB_JG_21_StoSettlePriceEntity cwbPriceEntity= cwb_JG_21_StoSettlePriceApp.GetEntity(cpkEntity.FProductionNO, cpkEntity.FProCompanyNO, cpkEntity.FProductionTypeNO);
                cpkEntity.FRegisterID = Guid.NewGuid().ToString("D");
                cpkEntity.UseUnit = useUnit;
                cpkEntity.FQuantity = cpkEntity.FQuantity_KP;
                cpkEntity.Fprice_Plane = priceEntity==null?0:priceEntity.Fprice_X;
                cpkEntity.FMoney_Plane = cpkEntity.Fprice_Plane * cpkEntity.FQuantity;
                cpkEntity.FPrice_Settle = cwbPriceEntity==null?0:cwbPriceEntity.Fprice;
                cpkEntity.FMoney_Settle = cpkEntity.FPrice_Settle * cpkEntity.FQuantity;
                cpkEntity.FCreateBy = userName;
                cpkEntity.FCreateDate = DateTime.Now.ToString("yyyy-MM-dd");
                cpkEntity.FCreateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Insert(cpkEntity);
            }
            else
            {
                CPK_51_JG_StoCPKPriceEntity priceEntity = cpk_51_JG_StoCPKPriceApp.GetEntity(cpkEntity.FProductionNO, cpkEntity.FProCompanyNO, cpkEntity.FProductionTypeNO);
                CWB_JG_21_StoSettlePriceEntity cwbPriceEntity = cwb_JG_21_StoSettlePriceApp.GetEntity(cpkEntity.FProductionNO, cpkEntity.FProCompanyNO, cpkEntity.FProductionTypeNO);

                cpkEntity.UseUnit = useUnit;
                cpkEntity.FUpdateBy = userName;
                cpkEntity.Fprice_Plane = priceEntity.Fprice_X;
                cpkEntity.FMoney_Plane = cpkEntity.Fprice_Plane * cpkEntity.FQuantity;
                cpkEntity.FPrice_Settle = cwbPriceEntity.Fprice;
                cpkEntity.FMoney_Settle = cpkEntity.FPrice_Settle * cpkEntity.FQuantity;
                cpkEntity.FUpdateDate = DateTime.Now.ToString("yyyy-MM-dd");
                cpkEntity.FUpdateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Update(cpkEntity);
            }
        }

        public void SaveStoreCell(List<CPK_11_DJ_StoRegisterEntity> list)
        {
            service.SaveStoreCell(list);
        }
    }
}
